//
//  PassengerView.swift
//  AirlineReservation
//
//  Created by Kevin on 2020/1/29.
//  Copyright © 2020 Kevin. All rights reserved.
//

import UIKit

class PassengerView: UIView {
    
    // MARK: 赋值
    public var userInfos:[UserInfo]? {
        didSet {
            updateLayout()
        }
    }
    // MARK: 回调
    public var complete:((_ item:Int) -> Void)?
    
    // 是否显示复选框
    public var isShowCheckBox: Bool = false
    
    public var checkBoxSelectTag = NSMutableDictionary()
    
    // scrollView
    private let scrollView = UIScrollView()
    
    @objc func clickCheckBox(btn:UIButton) -> Void {
        if btn.currentImage == UIImage(named: "IMG_CircleSelected_17x17") {
            checkBoxSelectTag.removeObject(forKey: "\(btn.tag)")
            btn.setImage(UIImage(named: "IMG_CircleUnSelected_17x17"), for: .normal)
        }else {
            checkBoxSelectTag.setValue(btn.tag, forKey: "\(btn.tag)")
            btn.setImage(UIImage(named: "IMG_CircleSelected_17x17"), for: .normal)
        }
    }
    
    override func layoutSubviews() {
        guard let passengers = userInfos else {
            return
        }
        scrollView.showsVerticalScrollIndicator = false
        scrollView.frame = CGRect(x: 0, y: 0, width: frame.width, height: frame.height)
        scrollView.contentSize = CGSize(width: SCREEN_WIDTH, height: CGFloat(passengers.count * 130))
        // 高度
        var tempHeight:CGFloat = 0
        for i in 0..<passengers.count {
            // 背景颜色
            let backround = UIView()
            // 姓名
            let usernameView = UILabel(title: "测试", color: UIColor.black, size: 18)
            usernameView.text = passengers[i].username
            usernameView.frame = CGRect(x: MARGIN, y: MARGIN, width: MARGIN140, height: MARGIN20)
            backround.addSubview(usernameView)
            // 编辑图片
            let editView = UIButton()
            editView.tag = i
            let editX = frame.width - MARGIN50
            editView.setImage(UIImage(named: "edit_15x15"), for: .normal)
            editView.frame = CGRect(x: editX, y: 0, width: MARGIN50, height: MARGIN50)
            backround.addSubview(editView)
            // 编辑图片 点击事件
            editView.addTarget(self, action: #selector(clickItem(btn:)), for: .touchUpInside)
            let emailY = editView.frame.maxY + MARGIN10
            // 复选框
            let checkBoxView = UIButton()
            if isShowCheckBox {
                checkBoxView.tag = i
                checkBoxView.frame = CGRect(x: 0, y: emailY - 17, width: 50, height: 50)
                checkBoxView.setImage(UIImage(named: "IMG_CircleUnSelected_17x17"), for: .normal)
                checkBoxView.addTarget(self, action: #selector(clickCheckBox(btn:)), for: .touchUpInside)
                backround.addSubview(checkBoxView)
            }
            // 身份证号
            let emailView = UILabel(title: "身份证/港澳台居住证", color: UIColor.gray, size: 13)
            if isShowCheckBox {
                emailView.frame = CGRect(x: checkBoxView.frame.maxX, y: emailY, width: 130, height: MARGIN)
            }else {
                emailView.frame = CGRect(x: MARGIN, y: emailY, width: 160, height: MARGIN)
            }
            backround.addSubview(emailView)
            // 身份证号输入
            let emailTextView = UILabel(title: "", color: COLOR_NICE, size: 13)
            emailTextView.text = passengers[i].idnumber
            let emialW = frame.width - emailView.frame.maxX - MARGIN
            emailTextView.frame = CGRect(x: emailView.frame.maxX, y: emailY, width: emialW, height: MARGIN)
            backround.addSubview(emailTextView)
            // 下划线
            let line = UIView()
            if i != passengers.count - 1 {
                line.backgroundColor = COLOR_LINE
            }
            let lineY = emailView.frame.maxY + MARGIN20
            line.frame = CGRect(x: MARGIN, y: lineY, width: frame.width - MARGIN * 2, height: 1)
            backround.addSubview(line)
            // 背景颜色
            backround.backgroundColor = UIColor.white
            backround.frame = CGRect(x: 0, y: tempHeight, width: frame.width, height: line.frame.maxY)
            scrollView.addSubview(backround)
            addSubview(scrollView)
            // 每条高度
            tempHeight += line.frame.maxY
        }
    }
    
    @objc func clickItem(btn:UIButton) -> Void {
        if let callback = complete {
            callback(btn.tag)
        }
    }
}
